package com.vanzo.some.interphone;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.vanzo.some.R;

/**
 * Created by tisen on 2017/12/27.
 */

public class ConnectAdapter extends RecyclerView.Adapter<ConnectItem> {

    private static final String TAG = "ConnectAdapter";

    private Context context;


    public void addConnect(Connect connect) {
        ConnectHandle.addConnect(connect);
        this.notifyDataSetChanged();
    }

    public void updateConnect(int position, Connect connect) {
        ConnectHandle.updateConnect(position, connect);
        this.notifyDataSetChanged();
    }

    public ConnectAdapter(Context context) {
        this.context = context;
    }


    @Override
    public ConnectItem onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ConnectItem(LayoutInflater.from(parent.getContext()).inflate(R.layout.connect_item, parent, false));
    }

    @Override
    public void onBindViewHolder(final ConnectItem holder, final int position) {
        holder.bind(ConnectHandle.getConnects(context).get(position));
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (itemOnClickListener != null) {
                    itemOnClickListener.onClick(holder.itemView, position);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return ConnectHandle.getConnects(context).size();
    }

    public void setItemOnClickListener(ItemOnClickListener itemOnClickListener) {
        this.itemOnClickListener = itemOnClickListener;
    }

    private ItemOnClickListener itemOnClickListener;

    public interface ItemOnClickListener {
        void onClick(View view, int position);
    }
}
